//
//  ECCalenderView.swift
//  summaryGraph
//
//  Created by jieliapp on 2017/2/23.
//  Copyright © 2017年 飞天江郎. All rights reserved.
//

import UIKit

class ECCalenderView: UIView,UITableViewDelegate,UITableViewDataSource {
    
    var cTableView:UITableView!
    
    var viewSW:CGFloat!
    var viewSH:CGFloat!
    
    var monthArray:Array<Date>!
    
    init(frame:CGRect,startDate:Date) {
        
        super.init(frame:frame)
        
        viewSW = frame.size.width
        viewSH = frame.size.height
        monthArray = CalenderToos.monthArrayFromDate(fromDate: startDate)
        
    
        let titleView  = UIView.init(frame: CGRect(x:0,y:0,width:viewSW,height:22))
        titleView.backgroundColor = UIColor.orange
        
        self.addSubview(titleView)
        self.addTitleLabs(view: titleView)
        
        cTableView = UITableView.init(frame: CGRect(x:0,y:22,width:viewSW,height:viewSH - 22))
        cTableView.rowHeight = 6 * (viewSW/7)
        cTableView.tableFooterView = UIView.init()
        cTableView.backgroundColor = UIColor.clear
        cTableView.dataSource = self
        cTableView.delegate = self
        cTableView.allowsSelection = false
        cTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        cTableView.register(ECCalenderCell.self, forCellReuseIdentifier: "monthCell")
        
        self.addSubview(cTableView)
        
        let deadlineTime = DispatchTime.now() + 0.1
        
        DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
            
            self.cTableView.setContentOffset(CGPoint.init(x: 0, y: self.cTableView.contentSize.height - self.cTableView.bounds.size.height), animated: true)
        }
    
    }
    
    /** TableViewDelegate **/
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let item = monthArray[indexPath.row]
        
        let d = CalenderToos.monthDayArrayFromDate(date: item)
        var cellSize:CGSize = CGSize.init(width: 0, height: 0)
        
        cellSize.width = viewSW
        
        if d.count>35{
            
            cellSize.height = viewSW
            
        }else{
            
            cellSize.height = 6 * (viewSW/7)
        }
        
        
        let cell = ECCalenderCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "monthCell", createDate: item,Cellsize: cellSize);
    
        
        
        return cell
        
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return monthArray.count
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let itemDate = monthArray[indexPath.row]
        
        let d = CalenderToos.monthDayArrayFromDate(date: itemDate)
        if d.count>35{
            
            return viewSW
            
        }else{
        
            return 6 * (viewSW/7)
        }
    }
    
    
    
    
    
    
    
    
    
    func addTitleLabs(view:UIView) {
        
        let k = viewSW/7
        for index in 0...6{
        let xWidth = CGFloat(index) * k
            let tapLab = UILabel.init(frame: CGRect(x:xWidth,y:0,width:k,height:22))
            tapLab.textColor = UIColor.black
            tapLab.text = switchWeekDaysStr(index: index)
            tapLab.textAlignment = .center
            view.addSubview(tapLab)
        }
        
    }
    
    
    
    func switchWeekDaysStr(index:Int) -> String {
    
        switch index {
        case 0:
            return "日"
        case 1:
            return "一"
        case 2:
            return "二"
        case 3:
            return "三"
        case 4:
            return "四"
        case 5:
            return "五"
        case 6:
            return "六"
        default:
            break
        }
        
        return " "
    
    }
    
    
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
